package com.d0327;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class Test2 {
    public static void main(String[] args) throws Exception{
        String s;
        BufferedReader br = null;
            String fileName1 = "src/com/d0327/java.txt";
            br = new BufferedReader(new FileReader(fileName1));
            StringBuffer sb = new StringBuffer();
            //将文件内容存入StringBuffer中
            while((s = br.readLine()) != null) {
                sb.append(s);
            }
            String str = sb.toString().toLowerCase();
            //分隔字符串并存入数组  (以，。空格分割)
            String[] elements = str.split("[,.\\s]");
            int count = 0;
            Map<String, Integer> myTreeMap = new TreeMap<String, Integer>();
            //遍历数组将其存入Map<String, Integer>中
            for(int i = 0; i < elements.length; i++) {
                if(myTreeMap.containsKey(elements[i])) {
                    count = myTreeMap.get(elements[i]);
                    myTreeMap.put(elements[i], count + 1);
                }
                else {
                    myTreeMap.put(elements[i], 1);
                }
            }
            /**
             * 直接遍历Map输出
             */
            System.out.println("直接遍历Map输出（无序）：");
            for(Map.Entry<String, Integer> entry:myTreeMap.entrySet()){
                System.out.println(entry.getKey()+":"+entry.getValue());
            }
            br.close();
    }

}
